using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//游戏入口脚本
public class GameApp : MonoBehaviour
{
    public static GameApp instance;
    private float _speed;
    public float speed{
        get { return _speed; }
        set { _speed = Mathf.Clamp(value, 1, 6); }
    }//移动速度
    public bool isDeath;
    public int explosionRange;//爆炸范围
    public int bombAmount;  // 炸弹数量
    public int bombsRemaining;  // 剩余炸弹数量
    private int _enemyCount;
    public int enemyCount
    {
        get { return _enemyCount; }
        set { _enemyCount = Mathf.Clamp(value, 1, 40); }
    } // 生成敌人数
    public bool enemyMove;  // 敌人移动
    public int deathEnemyCount;  // 消灭敌人数量
    public int level;  // 关卡数
    public int score; //分数
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            if (instance != this)
            {
                Destroy(gameObject);
            }
        }
        DontDestroyOnLoad(gameObject);
        Init();
        get();
        save();
    }

    private void Start()
    {
        //初始化声音管理器
        AudioManager.Instance.Init();
        //播放bgm
        AudioManager.Instance.PlayBGM("StartBackground");
        //显示loginUI界面
        UIManager.Instance.ShowUI<LoginUI>("开始界面");
    }

    //初始化
    public void Init()
    {
        Time.timeScale = 1;
        isDeath = false;
        speed = 2;
        explosionRange = 1;
        bombAmount = 1;
        bombsRemaining = 1;
        enemyCount = 1;
        deathEnemyCount = 0;
        level = 1;
        score = 0;
        enemyMove = true;
    }

    //读取
    public void get()
    {
        speed = PlayerPrefs.HasKey("speed") ? PlayerPrefs.GetFloat("speed") : 3;
        explosionRange = PlayerPrefs.HasKey("explosionRange") ? PlayerPrefs.GetInt("explosionRange") : 1;
        bombAmount = PlayerPrefs.HasKey("bombAmount") ? PlayerPrefs.GetInt("bombAmount") : 1;
        bombsRemaining = PlayerPrefs.HasKey("speed") ? PlayerPrefs.GetInt("speed") : 1;
        enemyCount = PlayerPrefs.HasKey("enemyCount") ? PlayerPrefs.GetInt("enemyCount") : 1;
        deathEnemyCount = PlayerPrefs.HasKey("deathEnemyCount") ? PlayerPrefs.GetInt("deathEnemyCount") : 0;
        level = PlayerPrefs.HasKey("level") ? PlayerPrefs.GetInt("level") : 1;
        score = PlayerPrefs.HasKey("score") ? PlayerPrefs.GetInt("score") : 0;
    }

    //存储数据
    public void save()
    {
        // 保存游戏进度
        PlayerPrefs.SetFloat("speed", speed);
        PlayerPrefs.SetFloat("explosionRange", explosionRange);
        PlayerPrefs.SetInt("bombAmount", bombAmount);
        PlayerPrefs.SetInt("bombsRemaining", bombsRemaining);
        PlayerPrefs.SetInt("enemyCount", enemyCount);
        PlayerPrefs.SetInt("enemyCount", enemyCount);
        PlayerPrefs.SetInt("level", level);
        PlayerPrefs.SetInt("score", score);
        PlayerPrefs.Save();
    }

    //清空数据
    public void del()
    {
        PlayerPrefs.DeleteAll();
    }
}
